//
//  LeanCloudLoginViewController.m
//  MPWeChatDemo
//
//  Created by HappyMilk 01 on 2017/5/1.
//  Copyright © 2017年 srulos. All rights reserved.
//

#import "LeanCloudLoginViewController.h"
#import "LeanCloudRegisterViewController.h"
#import "LoginHeader.h"
#import "QRCodeLoginViewController.h"
#import <AVOSCloud/AVOSCloud.h>
#import "ExplainViewController.h"


#define LOC_STR(str)  NSLocalizedStringFromTable(str, @"Login", nil)

@interface LeanCloudLoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *username_field;
@property (weak, nonatomic) IBOutlet UITextField *password_field;
@property (weak, nonatomic) IBOutlet UIButton *login_btn;
@property (weak, nonatomic) IBOutlet UIButton *wx_login_btn;
@property (weak, nonatomic) IBOutlet UILabel *socia_login_label;
@property (weak, nonatomic) IBOutlet UIButton *registerBtn;
@property (weak, nonatomic) IBOutlet UILabel *top_label;

@end

@implementation LeanCloudLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initNavigationBar];
    [self initViews];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textfildDidChange) name:UITextFieldTextDidChangeNotification object:nil];
    ADD_OBSERVER(self, @selector(onAppInfoLoadedCB:), NOTIFICATION_APP_INFO_LOADED, nil);
    [self showWXLoginBtnIfNeed];
}

- (void)onAppInfoLoadedCB:(id)sender{
    [self showWXLoginBtnIfNeed];
}

-(void)viewDidAppear:(BOOL)animated{
    if([MPWeChat stander].m_isLogin)
      [ self.navigationController dismissViewControllerAnimated:true completion:nil];
}

-(void)initNavigationBar{
    self.title = LOC_STR(@"Login");
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:LOC_STR(@"cancle") style:UIBarButtonItemStylePlain target:self action:@selector(onDismissCallBack)];
    
    self.navigationController.navigationBar.barTintColor = _mainColor;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil];
    [self.navigationController.navigationBar setTitleTextAttributes:attributes];
}

-(void)initViews{
    _login_btn.layer.cornerRadius = 5;
    _socia_login_label.text = LOC_STR(@"———— Social account login ————");
    [_login_btn setTitle:LOC_STR(@"Login") forState:0];
    _top_label.text = LOC_STR(@"Please Login");
    [_registerBtn setTitle:LOC_STR(@"Register") forState:0];
    
 
    UIImageView *userNameImage= [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"login_username.png"]];
    userNameImage.frame = CGRectMake(10, 8, 15, 15);
    userNameImage.contentMode= UIViewContentModeScaleAspectFit;
    UIView *userNameLeftView = [[UIView alloc]initWithFrame:CGRectMake(10, 0, 25, 30)];
    [userNameLeftView addSubview:userNameImage];
    self.username_field.leftView = userNameLeftView;
    self.username_field.leftViewMode = UITextFieldViewModeAlways;
    self.username_field.placeholder = LOC_STR(@"account number");
    
    UIImageView *passwordImage=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"login_psd.png"]];
    passwordImage.frame=CGRectMake(10, 8, 15, 15);
    passwordImage.contentMode=UIViewContentModeScaleAspectFit;
    UIView *passwordLeftView=[[UIView alloc]initWithFrame:CGRectMake(10, 0, 25, 30)];
    [passwordLeftView addSubview:passwordImage];
    self.password_field.leftView=passwordLeftView;
    self.password_field.leftViewMode=UITextFieldViewModeAlways;
    self.password_field.placeholder = LOC_STR(@"password");

}


-(void)showWXLoginBtnIfNeed{
    _wx_login_btn.hidden = ![MPWeChat stander].m_needLogin;
    _socia_login_label.hidden = ![MPWeChat stander].m_needLogin;
}

-(void)textfildDidChange{
    if(_username_field.text.length<6 || _password_field.text.length<6)
        _login_btn.backgroundColor = [UIColor colorWithRed:220/255.0 green:220/255.0 blue:220/255.0 alpha:1];
    else
        _login_btn.backgroundColor = self.mainColor;

}


- (IBAction)onRegister:(id)sender {
    if ([MPWeChat stander].m_needLogin) {
        UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"服务器维护暂时停用注册功能！老用户可正常登录！新用户请使用第三方登录！" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertview show];
        return;
    }
    
    LeanCloudRegisterViewController * leanCloudRegisterViewController = [[LeanCloudRegisterViewController alloc]initWithNibName:@"LeanCloudRegisterViewController" bundle:nil];
    leanCloudRegisterViewController.mainColor = _mainColor;
    [self.navigationController pushViewController:leanCloudRegisterViewController animated:true];
}

- (IBAction)onLogin:(UIButton *)sender {
    NSString *username = _username_field.text;
    NSString *password = _password_field.text;
    
    if(username.length<6 || password.length <6 ){
        NSString *msg = LOC_STR(@"Please fill in the correct account or password! Account password must be more than 6!");
        SHOW_HUD_MESSAGE(msg);
        return;
    }
    
    SHOW_HUD(LOC_STR(@"logging in..."));
    [AVUser logInWithUsernameInBackground:username password:password block:^(AVUser *user, NSError *error) {
        HIDE_HUD
        if (user != nil) {
            [MPWeChat stander].m_isLogin = true;
            [self dismissViewControllerAnimated:true completion:nil];
            
            UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"" message:LOC_STR(@"login successful") delegate:nil cancelButtonTitle:LOC_STR(@"Confirm") otherButtonTitles:nil, nil];
            [alertview show];
            
        } else {
            SHOW_HUD_MESSAGE(LOC_STR(@"Username or password is wrong!"));
        }
        
    }];
}

-(void)onDismissCallBack{
    [self dismissViewControllerAnimated:true completion:nil];
}

- (IBAction)onWXLogin:(id)sender {
    QRCodeLoginViewController * qrCodeLoginViewController = [[QRCodeLoginViewController alloc]initWithNibName:@"QRCodeLoginViewController" bundle:nil];
    [self presentViewController:qrCodeLoginViewController animated:true completion:nil];
    
}


- (IBAction)onPrivacyPolicyBtn:(UIButton *)sender {
    
    ExplainViewController * explainViewController = [[ExplainViewController alloc]initWithNibName:@"ExplainViewController" bundle:nil];
    [self.navigationController pushViewController:explainViewController animated:YES];
}

@end
